home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AppsToGo / DTS.Draw / Clipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  7.2 KB  |  242 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Clipboard.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* This file contains the code for the document procedure pointers for the DTS.Draw
  20. ** clipboard document.  The clipboard document is simply a modified DTS.Draw document.
  21. ** Many of the main document facilities are removed, since they don't apply to the
  22. ** clipboard.  See ClipboardInitDocument() for a full breakdown of the changes in
  23. ** the document procedures. */
  24.  
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30.  
  31. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  32. #include "App.defs.h"        /* Get various application definitions.            */
  33. #include "App.protos.h"        /* Get the prototypes for the application.        */
  34.  
  35. #ifndef __ERRORS__
  36. #include <Errors.h>
  37. #endif
  38.  
  39. #ifndef __TREEOBJ2__
  40. #include "TreeObj2.h"
  41. #endif
  42.  
  43.  
  44.  
  45. /*****************************************************************************/
  46.  
  47.  
  48.  
  49. extern RgnHandle    gCursorRgn;
  50. extern CursPtr        gCursorPtr;
  51.  
  52. static OSErr    ClipboardInitContent(FileRecHndl frHndl, WindowPtr window);
  53. static void        ClipboardContentClick(WindowPtr window, EventRecord *event, Boolean firstClick);
  54. static Boolean    ClipboardContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough);
  55. static Boolean    ClipboardWindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt);
  56.  
  57.  
  58.  
  59. /*****************************************************************************/
  60. /*****************************************************************************/
  61.  
  62. #ifdef applec
  63. #pragma segment DTSDrawSeg1
  64. #endif
  65.  
  66. /*****************************************************************************/
  67. /*****************************************************************************/
  68.  
  69.  
  70.  
  71. /* Initialize the clipboard document. */
  72.  
  73. OSErr    ClipboardInitDocument(FileRecHndl frHndl)
  74. {
  75.     OSErr        err;
  76.     FileRecPtr    frPtr;
  77.  
  78.     err = DefaultInitDocument(frHndl, 0, 0, 0);
  79.         /* Use the hierarchical document architecture. */
  80.  
  81.     if (!err) {        /* Set some custom window behaviors, and disable the rest. */
  82.         frPtr = *frHndl;
  83.         frPtr->fileState.calcFrameRgnProc        = nil;
  84.         frPtr->fileState.contentClickProc        = ClipboardContentClick;
  85.         frPtr->fileState.contentKeyProc          = ClipboardContentKey;
  86.         frPtr->fileState.drawFrameProc           = nil;
  87.         frPtr->fileState.freeDocumentProc        = nil;
  88.         frPtr->fileState.freeWindowProc          = nil;
  89.         frPtr->fileState.initContentProc         = ClipboardInitContent;
  90.         frPtr->fileState.readDocumentProc        = nil;
  91.         frPtr->fileState.readDocumentHeaderProc  = nil;
  92.         frPtr->fileState.resizeContentProc       = nil;
  93.         frPtr->fileState.scrollFrameProc         = nil;
  94.         frPtr->fileState.undoFixupProc           = nil;
  95.         frPtr->fileState.windowCursorProc        = ClipboardWindowCursor;
  96.         frPtr->fileState.writeDocumentProc       = nil;
  97.         frPtr->fileState.writeDocumentHeaderProc = nil;
  98.     }
  99.  
  100.     return(err);
  101. }
  102.  
  103.  
  104.  
  105. /*****************************************************************************/
  106.  
  107.  
  108.  
  109. /* Initialize the clipboard document size.  By waiting this late to state the
  110. ** document size, the window is initially opened to the size described in the
  111. ** 'WIND' resource for the clipboard.  Once the window exists, we can then
  112. ** set the document size to be 7 inches by 10 inches. */
  113.  
  114. static OSErr    ClipboardInitContent(FileRecHndl frHndl, WindowPtr window)
  115. {
  116. #ifndef __MWERKS__
  117. #pragma unused (window)
  118. #endif
  119.  
  120.     SetDocSize(frHndl, (7 * 72), (10 * 72));
  121.     return(noErr);
  122. }
  123.  
  124.  
  125.  
  126. /*****************************************************************************/
  127. /*****************************************************************************/
  128.  
  129.  
  130.  
  131. /* Handle only document scrolling for the clipboard. */
  132.  
  133. static void    ClipboardContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
  134. {
  135. #ifndef __MWERKS__
  136. #pragma unused (frHndl, firstClick)
  137. #endif
  138.  
  139.     IsCtlEvent(window, event, nil, nil);
  140. }
  141.  
  142.  
  143.  
  144. /*****************************************************************************/
  145.  
  146.  
  147.  
  148. /* No keys for the clipboard.  Returning true eats all of the keys, so that
  149. ** they aren't passed on to the next window behind the clipboard. */
  150.  
  151. static Boolean    ClipboardContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough)
  152. {
  153. #ifndef __MWERKS__
  154. #pragma unused (window, event, passThrough)
  155. #endif
  156.  
  157.     return(true);
  158. }
  159.  
  160.  
  161.  
  162. /*****************************************************************************/
  163.  
  164.  
  165.  
  166. /* Whenever the clipboard is active, just use an arrow cursor. */
  167.  
  168. static Boolean    ClipboardWindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt)
  169. {
  170. #ifndef __MWERKS__
  171. #pragma unused (frHndl, window, globalPt)
  172. #endif
  173.  
  174.     SetCursor(gCursorPtr = &qd.arrow);
  175.     return(true);
  176. }
  177.  
  178.  
  179.  
  180. /*****************************************************************************/
  181. /*****************************************************************************/
  182.  
  183.  
  184.  
  185. /* Handle cut/copy/paste for the clipboard. */
  186.  
  187. void    DoClipboard(FileRecHndl frHndl, short menuItem)
  188. {
  189.     FileRecHndl    frClip;
  190.     TreeObjHndl    root, rootClip, chndl;
  191.     short        cnum;
  192.     WindowPtr    clipWind, window;
  193.  
  194.     if (!(clipWind = GetNextWindow(nil, kClipboardFileType))) return;
  195.  
  196.     frClip   = (FileRecHndl)GetWRefCon(clipWind);
  197.     rootClip = (*frClip)->d.doc.root;
  198.     root     = (*frHndl)->d.doc.root;
  199.  
  200.     if (menuItem != kStdCopy)
  201.         NewDocumentUndo(frHndl);
  202.  
  203.     switch (menuItem) {
  204.         case kStdCut:
  205.         case kStdCopy:
  206.             while ((*rootClip)->numChildren) DisposeChild(NO_EDIT, rootClip, 0);
  207.                 /* First dispose of the clipboard's old content. */
  208.             for (cnum = (*root)->numChildren; cnum;) {
  209.                 chndl = GetChildHndl(root, --cnum);
  210.                 if (mDerefCommon(chndl)->selected) {    /* If document object is selected...    */
  211.                     CopyChild(NO_EDIT, root, cnum, rootClip, 0, true);            /* Copy it.     */
  212.                     mDerefCommon(GetChildHndl(rootClip, 0))->selected = false;    /* Deselect it. */
  213.                     mDerefRoot(rootClip)->numSelected = 0;
  214.                         /* Copying the child caused it to get selected, and to increment the
  215.                         ** numSelected value in the clipboard's root.  Reset it. */
  216.                 }
  217.             }
  218.             if (((WindowPeek)clipWind)->visible) {        /* If clipboard visible... */
  219.                 BeginContent(clipWind);        /* Redraw clipboard to show the new content. */
  220.                 DoImageDocument(frClip);
  221.                 EndContent(clipWind);
  222.             }
  223.             if (menuItem == kStdCut)    /* If clipboard operation was a delete... */
  224.                 DoDelete(frHndl);        /* Delete the objects out of the document. */
  225.             break;
  226.         case kStdPaste:
  227.             DoTreeSelect(root, SELECTOFF);        /* Turn off current document selections. */
  228.             for (cnum = (*rootClip)->numChildren; cnum;) {
  229.                 chndl = GetChildHndl(rootClip, --cnum);
  230.                 CopyChild(CLIPBOARD_EDIT, rootClip, cnum, root, 0, true);
  231.                     /* Copy the clipboard objects with deepCopy to copy grouped objects as well. */
  232.             }
  233.             BeginContent(window = (*frHndl)->fileState.window);        /* Redraw document. */
  234.             DoImageDocument(frHndl);
  235.             EndContent(window);
  236.             break;
  237.     }
  238. }
  239.  
  240.  
  241.  
  242.